package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.TeachingGroupVO;
import edu.zqu.instructionalMatters.project.entity.TeachingGroup;
import edu.zqu.instructionalMatters.project.mapper.TeachingGroupMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import edu.zqu.instructionalMatters.project.service.CourseService;
import edu.zqu.instructionalMatters.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 课程组 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/teachingGroup")
@Api(tags = "课程组")
public class TeachingGroupController {
    @Autowired
    private TeachingGroupMapper teachingGroupMapper;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取课程组列表")
    @RequestMapping(value = "/teachingGroups", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId,
                             BigInteger courseId,
                             BigInteger teacherId
    ) {
        TeachingGroup teachingGroup1 = new TeachingGroup()
                .setCollegeId(collegeId).setCourseId(courseId).setTeacherId(teacherId);                                 //合成对象
        List<TeachingGroup> list1 = teachingGroupMapper.findAll(teachingGroup1);                                        //获取 TeachingGroupLeader 集合
        List<TeachingGroupVO> list = new ArrayList<>();                                                                 //初始化 TeachingGroupLeaderVO 集合
        for (TeachingGroup teachingGroup : list1) {                                                                      //循环遍历
            TeachingGroupVO teachingGroupVO = new TeachingGroupVO();                                                    //初始化 TeachingGroupLeaderVO
            BeanUtils.copyProperties(teachingGroup, teachingGroupVO);                                                    //对象复制赋值
            String collegeName = collegeService.getCollegeName(teachingGroup.getCollegeId());                           //获取学校名称
            String courseName = courseService.getCourseName(teachingGroup.getCourseId());                               //获取课程名称
            String teacherName = userService.getUserName(teachingGroup.getTeacherId());                                 //获取教师名称
            teachingGroupVO.setCollegeName(collegeName);                                                                //设置学校名称
            teachingGroupVO.setCourseName(courseName);                                                                  //设置课程名称
            teachingGroupVO.setTeacherName(teacherName);                                                                //设置教师名称
            list.add(teachingGroupVO);                                                                                  //加入 list
        }
        return ZQUResult.success(list);                                                                                 //返回 list
    }

    @ApiOperation(value = "获取课程组信息", response = TeachingGroupVO.class)
    @GetMapping(value = "/teachingGroup/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        TeachingGroup teachingGroup = teachingGroupMapper.selectById(id);                           //查询数据对象
        String collegeName = collegeService.getCollegeName(teachingGroup.getCollegeId());           //获取学校名称
        String courseName = courseService.getCourseName(teachingGroup.getCourseId());               //获取课程名称
        String teacherName = userService.getUserName(teachingGroup.getTeacherId());                 //获取教师名称
        TeachingGroupVO teachingGroupVO = new TeachingGroupVO();                                    //初始化VO
        BeanUtils.copyProperties(teachingGroup, teachingGroupVO);                                    //对象复制赋值
        teachingGroupVO.setCollegeName(collegeName);                                                //设置学校名称
        teachingGroupVO.setCourseName(courseName);                                                  //设置课程名称
        teachingGroupVO.setTeacherName(teacherName);                                                //设置教师名称
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", teachingGroupVO);                 //成功返回数据对象
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/teachingGroup", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "beginTime", value = "开始时间", paramType = "query", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger teacherId,
                         BigInteger courseId,
                         Date beginTime,
                         Date endTime,
                         BigInteger byWho) {
        TeachingGroup teachingGroup = new TeachingGroup();                     //初始化
        teachingGroup.setCollegeId(collegeId);                                 //设置学校id
        teachingGroup.setTeacherId(teacherId);                                 //设置教师id
        teachingGroup.setCourseId(courseId);                                   //设置课程id
        teachingGroup.setBeginTime(beginTime);                                 //设置开始担任课程组成员的时间
        teachingGroup.setEndTime(endTime);                                     //设置结束担任课程组成员的时间
        teachingGroup.setByWho(byWho);                                         //设置操作者
        teachingGroupMapper.insert(teachingGroup);                             //执行插入
        return ZQUResult.success();                                            //操作成功返回
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/teachingGroup/{id}", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "beginTime", value = "开始时间", paramType = "query", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger teacherId,
                            BigInteger courseId,
                            Date beginTime,
                            Date endTime,
                            BigInteger byWho) {
        TeachingGroup teachingGroup = teachingGroupMapper.selectById(id);      //先查出旧纪录
        teachingGroup.setByWho(byWho);                                         //修改操作者
        teachingGroupMapper.updateById(teachingGroup);                         //更新操作者
        teachingGroupMapper.deleteById(id);                                    //逻辑删除
        teachingGroup = new TeachingGroup();                                   //初始化新记录
        teachingGroup.setCollegeId(collegeId);                                 //设置学校id
        teachingGroup.setTeacherId(teacherId);                                 //设置教师id
        teachingGroup.setCourseId(courseId);                                   //设置课程id
        teachingGroup.setBeginTime(beginTime);                                 //设置开始担任课程组成员的时间
        teachingGroup.setEndTime(endTime);                                     //设置结束担任课程组成员的时间
        teachingGroup.setByWho(byWho);                                         //设置操作者
        teachingGroup.setLastVersion(id);                                      //设置上一版本
        teachingGroupMapper.insert(teachingGroup);                             //执行插入
        return ZQUResult.success();                                            //操作成功返回
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/teachingGroup/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        TeachingGroup teachingGroup = teachingGroupMapper.selectById(id);      //先查出旧纪录
        teachingGroup.setByWho(byWho);                                         //修改操作者
        teachingGroupMapper.updateById(teachingGroup);                         //更新操作者
        teachingGroupMapper.deleteById(id);                                    //逻辑删除
        return ZQUResult.success();                                            //操作成功返回
    }
}

